package com.xckj.teacher.ui.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.xckj.teacher.R;
import com.xckj.teacher.bean.TextbookBean;

import java.util.List;

/**
 * @Author: ChenYao
 * @Describe: 版本名字
 * @Date: 2024/6/11 23:29
 */
public class VersionNameAdapter extends RecyclerView.Adapter {
    private List<TextbookBean.Children> childrenList;

    private OnItemClickListener onItemClickListener;

    @SuppressLint("NotifyDataSetChanged")
    public void setData(List<TextbookBean.Children> childrenList) {
        this.childrenList = childrenList;
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ItemViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_version_name, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
       ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
        TextbookBean.Children children = childrenList.get(position);
        itemViewHolder.itemView.setOnClickListener(view -> {
            if (null != onItemClickListener) {
                onItemClickListener.onItemClick(children);
            }
        });
        itemViewHolder.tvVersionName.setText(children.book_name);
    }

    @Override
    public int getItemCount() {
        return childrenList == null ? 0 : childrenList.size();
    }

    static class ItemViewHolder extends RecyclerView.ViewHolder {
        private final TextView tvVersionName;

        public ItemViewHolder(@NonNull View itemView) {
            super(itemView);
            tvVersionName = itemView.findViewById(R.id.tv_version_name);
        }
    }

    public interface OnItemClickListener {
        /**
         * Item的点击事件
         *
         * @param children 当前选中的数据
         */
        void onItemClick(TextbookBean.Children children);
    }
}

